<template>
	<view class="wrap">
		<u-gap height="20" bg-color="#f5f5f5"></u-gap>
		<div class="version-logo"><image src="/static/assets/images/logo.png" class="logo-image" /></div>
    <div class="version-text" @click="handleClick">当前版本 {{ version }}</div>
    <div class="version-update">
      <u-cell-group class="cell-group">
        <u-cell-item title="检查更新" @click="checkUpdate" :arrow="true"></u-cell-item>
      </u-cell-group>
    </div>
	</view>
</template>

<script>
import { TimesTrigger, loger } from '@daelui/dogjs/dist/components'
import { queryAppVersion } from '@/api/system/app.js'
import { store } from '@/api/service/store-service.js'
import { confirmUpgrade } from '@/components/upgrade/upgrade.vue'

export default {
	data() {
		return {
			version: '1.0.0'
		}
	},

  onShow () {
		const systemInfo = uni.getSystemInfoSync()
		const currentVersion = systemInfo.appVersion
    this.version = currentVersion
  },

	methods: {
		// 检查更新
		checkUpdate() {
			// #ifdef APP-PLUS
			queryAppVersion().then(res => {
				res = res || {}
				// 本地版本与查询的版本不一致则提示更新
				if (String(res.code) === '200' || !res.data) {
					const systemInfo = uni.getSystemInfoSync()
					const currentVersionCode = parseFloat(systemInfo.appVersionCode || 0)
					if (!res.data?.version || currentVersionCode >= res.data?.version) {
						this.$u.toast('已是最新版本，无需更新')
						return true
					}
					// 下载更新
					store.setItem('upgrade-data', res.data)
					// 确认
					confirmUpgrade().then(b => {
						b && uni.navigateTo({
							url: '/pages/sys/user/upgrade?source=version'
						})
					})
				} else {
					this.$u.toast(res.message || '检查更新失败')
				}
			}).catch((e) => {
				console.log(e)
				this.$u.toast('检查更新失败')
			})
			// #endif
			// #ifndef APP-PLUS
			this.$u.toast('小程序端或H5端无需检查更新')
			// #endif
		},
		// Debug模式触发
		handleClick () {
      let tt = this.tt
      if (!tt) {
        tt = new TimesTrigger({
          times: 5
        })
        this.tt = tt
      }
      tt.trigger(function() {
        uni.navigateTo({
          url: '/pages/sys/workbench/debug'
        })
      })
    }
	}
};
</script>
<style lang="scss">
@import '../home/index.scss';
page {
	background-color: #fff;
}
.version-logo {
  padding: 16px 0;
  text-align: center;
  .logo-image {
    max-width: 80%;
    // width: 128px;
    // height: 128px;
    width: 86px;
    height: 86px;
  }
}
.version-text {
  padding: 8px 0;
  text-align: center;
}
.version-update {
  margin-top: 24px;
  padding: 16px 0;
}
</style>
